<?php

class ReportController extends AdminBase{
	public function actionIndex(){

		//默认的没有其它搜索条件选择时间是昨天凌晨开始
		$start_time = empty($_GET['start_time']) ? 0 : strtotime($_GET['start_time']);
		$end_time = empty($_GET['end_time']) ? 0 : strtotime($_GET['end_time']) + 86400;
		$this->assign($_GET);
        if(!empty($_GET['search_time'])){
            switch ($_GET['search_time']) {
                case 1:
                $start_time = strtotime('today'); //今天凌晨开始算
                $end_time = 0;
                    break;
                case 2:
                $start_time = strtotime('yesterday');//昨天凌晨到今天的凌晨的时间
                $end_time = strtotime('today');
                    break;
                case 3:
                $start_time = strtotime('today') - 7*86400;//今天凌晨往前推一个星期
                $end_time = strtotime('today');
                    break;                
            }
        }
        if(empty($start_time) && empty($end_time)){
        	$_GET['search_time'] = 1;
            $start_time = strtotime('today'); //今天凌晨开始算
            $end_time = 0;        	
        }
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
/*----------------------------------------------------------------------------------------------------*/

		//根据时间范围选取各个类目数据
		$select = 'admin_id,cat_id,status,isdelivery';
		$arrGoodsDatas = GoodsBmModel::model()->getDataForAdd_time($start_time, $end_time, 'status_savetime', $select, 'admin_id > 0');

/*----------------------------------------------------------------------------------------------------*/		
		//计算统计结果
		$arrData = array();//统计结果
		$arrCount = array();//总的统计
		
		//获取所有后台用户相关的信息
		$arrUserList = AdminUser::model()->getUserList();
		foreach($arrGoodsDatas as $arrGoodsData){ 
			//获取用户名
			if(empty($arrData[$arrGoodsData['admin_id']])){
				$arrData[$arrGoodsData['admin_id']]['username'] = $arrUserList[$arrGoodsData['admin_id']]['real_name'];
			}

			//审核通过数
			if($arrGoodsData['status'] == 1){
				$arrCount['sum_pass']++;
				$arrData[$arrGoodsData['admin_id']]['sum_pass']['count']++; //个人审核通过数
				$arrData[$arrGoodsData['admin_id']]['sum_pass']['cat'][$arrGoodsData['cat_id']]++; //某个分类审核通过数
			}
			//审核通过且是已邮寄样品
			if($arrGoodsData['status'] == 1 && $arrGoodsData['isdelivery'] > 0){
				$arrCount['sum_pass_delivery']++;
				$arrData[$arrGoodsData['admin_id']]['sum_pass_delivery']['count']++; //审核通过且是已邮寄样品
				$arrData[$arrGoodsData['admin_id']]['sum_pass_delivery']['cat'][$arrGoodsData['cat_id']]++; //审核通过且是已邮寄样品
			}
			//商家邮寄样品数
			if($arrGoodsData['isdelivery'] == 1){
				$arrCount['sum_delivery']++;
				$arrData[$arrGoodsData['admin_id']]['sum_delivery']['count']++; //个人邮寄样品数
				$arrData[$arrGoodsData['admin_id']]['sum_delivery']['cat'][$arrGoodsData['cat_id']]++;//个人某个分类邮寄样品数
			}

			//商家已邮寄样品，我们收到了
			if($arrGoodsData['isdelivery'] == 2){
				$arrCount['sum_has_delivery']++;
				$arrData[$arrGoodsData['admin_id']]['sum_has_delivery']['count']++; //个人邮寄样品数
				$arrData[$arrGoodsData['admin_id']]['sum_has_delivery']['cat'][$arrGoodsData['cat_id']]++;//个人某个分类邮寄样品数
			}
		}
/*----------------------------------------------------------------------------------------------------*/


		//每个审核人员的历史数据
		$time = time() - 86400*60;
		$this->assign('history_time', $time);
		$objCriteria = new CdbCriteria(array(
			'condition' => "admin_id > 0 and status = 3 and isdelivery = 2 and status_savetime > $time",
			'select' => 'admin_id,cat_id,status,isdelivery',
		));
		$objModel = new GoodsBmModel();
		$command=$objModel->getCommandBuilder()->createFindCommand($objModel->getTableSchema(),$objCriteria,$objModel->getTableAlias());        
		$arrGoodsDatas = $command->queryAll();
		foreach($arrGoodsDatas as $arrGoodsData){
			//获取用户名
			if(empty($arrData[$arrGoodsData['admin_id']])){
				$arrData[$arrGoodsData['admin_id']]['username'] = $arrUserList[$arrGoodsData['admin_id']]['real_name'];
			}
			$arrCount['sum_not']++;
			$arrData[$arrGoodsData['admin_id']]['sum_not']['count']++; //个人审核通过数
			$arrData[$arrGoodsData['admin_id']]['sum_not']['cat'][$arrGoodsData['cat_id']]++; //某个分类审核通过数						
		}

/*----------------------------------------------------------------------------------------------------*/
		foreach($arrData as $key => $data){
			if(empty($data['sum_pass']) && empty($data['sum_pass_delivery']) && empty($data['sum_delivery']) && empty($data['sum_has_delivery']) && empty($data['sum_not'])){
				unset($arrData[$key]);
				continue;
			}
		}

		//$this->assign($_GET);
		$this->assign('count', $arrCount);
		$this->assign('data', $arrData);
        $this->render('index');   		
	}


	public function actionGoodsList(){
        $objModel = new GoodsBmModel('search');
        $objModel->resetAttributes();
        if(isset($_GET))
        {
            $objModel->attributes = $_GET;
        }
        $objCriteria = $objModel->createSearchCriteria();

        $this->basePage('GoodsBmModel', $objCriteria); 

        $this->render();     		
	}

	public function actionCountproduct(){
		//设置搜索条件
		$objModel = new GoodsBmModel();
        $objCriteria = $this->setSearch($objModel);
        $objCriteria->addCondition('product_id > 0');
        $objCriteria->group = 'product_id';
        $objCriteria->select = 'product_id,product_img,sum(case when status=1 then 1 else 0 end) sum_pass, count(*) sum';

        //分页
        $intCount = $objModel->count($objCriteria);
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        $limit = 20;
        $page = self::page($intCount, $limit, $pageId);
        $strPage = $page->show();
        $objCriteria->limit = $page->listRows;
        $objCriteria->offset = $page->firstRow;

        //排序
        $objSort = new CSort( 'GoodsBmModel' );
        //$objSort->defaultOrder = 'id DESC';
        $objSort->applyOrder( $objCriteria );


        //通过数组返回，获取需要统计的数据
		$command=$objModel->getCommandBuilder()->createFindCommand($objModel->getTableSchema(),$objCriteria,$objModel->getTableAlias());        
		$arrGoodsDatas = $command->queryAll(); //得到相关的所有的数据
		$arrResult = array();

		$objModel = new GuestFenxiModel();
		$arrProduct_id = array();
		foreach($arrGoodsDatas as $key => $arrGoodsData){
			$arrProduct_id[] = $arrGoodsData['product_id'];
		}
		$strProduct_id = implode(',', $arrProduct_id);
		$objCriteria = new CdbCriteria(array(
			'condition' => "product_id in ($strProduct_id)",
			'group' => 'product_id',
			'select' => 'product_id,sum(sales) sales,sum(turnover) turnover',//成交总额与成交量,
		));
        //通过数组返回，获取需要统计的数据
		$command=$objModel->getCommandBuilder()->createFindCommand($objModel->getTableSchema(),$objCriteria,$objModel->getTableAlias());        
		$arrResult = $command->queryAll(); //得到相关的所有的数据
		foreach ($arrResult as $keys => $values) {
			foreach($arrGoodsDatas as $key => $value){
				if($values['product_id'] == $value['product_id']){
					$arrGoodsDatas[$key]['sales'] = $values['sales'];
					$arrGoodsDatas[$key]['turnover'] = $values['turnover'];
				}
			}
		}

		$this->assign($_GET);
		$this->assign('count', $intCount);
		$this->assign('Page', $strPage);
		$this->assign('data', $arrGoodsDatas);
        $this->render();   			

	}


	public function setSearch($objModel){
		$strClassNmae = get_class($objModel);
		$objModel->resetAttributes();
        if(isset($_GET[$strClassNmae]))
        {
            $objModel->attributes = $_GET[$strClassNmae];
        }
        $objCriteria = $objModel->createSearchCriteria();
        return 	$objCriteria;	
	}

	//统计销量与上架数。。。
	public function actionCountsale(){

		//默认的没有其它搜索条件选择时间是昨天凌晨开始
		if(empty($_GET['start_time']) && empty($_GET['end_time'])){
            $start_time = strtotime('yesterday');//昨天凌晨到今天的凌晨的时间
            $end_time = strtotime('today');            
		}else{
			$start_time = empty($_GET['start_time']) ? 0 : strtotime($_GET['start_time']);
			$end_time = empty($_GET['end_time']) ? 0 : strtotime($_GET['end_time']) + 86400;
		}

/*----------------------------------------------------------------------------------------------------*/

		//根据时间范围选取各个类目数据
		$select = 'admin_id,cat_id,status,isdelivery,start_time,end_time,promo_price';
		$arrDatas['add_time'] = GoodsBmModel::model()->getDataForAdd_time($start_time, $end_time, 'add_time', $select);
		$arrDatas['status_savetime'] = GoodsBmModel::model()->getDataForAdd_time($start_time, $end_time, 'status_savetime', $select);
		$arrDatas['start_time'] = GoodsBmModel::model()->getDataForAdd_time($start_time, $end_time, 'start_time', $select, "status = 1");

		$select = 'admin_id,cat_id,turnover,sales,goods_id,product_id,logtime,(sales/turnover) promo_price';
		$arrDatas['logtime'] = GoodsBmModel::model()->getDataForAdd_time($start_time, $end_time, 'logtime', $select, "", 'GuestFenxiModel');

/*----------------------------------------------------------------------------------------------------*/
		
		$arrResult = array();//统计结果
		
		$arrUserList = AdminUser::model()->getUserList(); //获取用户名
		$arrCategoryList = CategoryModel::model()->getListArray(); //获取分类的名字
/*----------------------------------------------------------------------------------------------------*/
		//统计关于报名的
		foreach($arrDatas['add_time'] as $arrData){
			//获取类目
			if(empty($arrResult[$arrData['cat_id']])){
				$arrResult[$arrData['cat_id']]['name'] = $arrCategoryList[$arrData['cat_id']];
			}				
			//获取报名商品总数
			$arrResult[$arrData['cat_id']]['count']++;
			if(!$arrData['admin_id']){
				continue;
			}	
			//获取用户名
			if(empty($arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['username'])){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['username'] = $arrUserList[$arrData['admin_id']]['real_name'];
			}					
		}
		unset($arrDatas['add_time']);
/*----------------------------------------------------------------------------------------------------*/
		//统计关于状态的		
		foreach($arrDatas['status_savetime'] as $arrData){
			//获取类目
			if(empty($arrResult[$arrData['cat_id']])){
				$arrResult[$arrData['cat_id']]['name'] = $arrCategoryList[$arrData['cat_id']];
			}
			if(!$arrData['admin_id']){
				continue;
			}				
			//获取用户名
			if(empty($arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['username'])){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['username'] = $arrUserList[$arrData['admin_id']]['real_name'];
			}
			//待审核
			if($arrData['status'] == 0 && $arrData['admin_id'] > 0){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['status_0']++;
			}
			//审核通过
			if($arrData['status'] == 1 && $arrData->admin_id > 0){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['status_1']++;
			}
			//审核不通过
			if($arrData['status'] == 2 && $arrData->admin_id > 0){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['status_2']++;
			}
			//邮寄样品
			if($arrData['isdelivery'] > 0){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['isdelivery_12']++;
			}
			//收到样品
			if($arrData['isdelivery'] == 2){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['isdelivery_2']++;
			}
		}
		unset($arrDatas['status_savetime']);
/*----------------------------------------------------------------------------------------------------*/
		//统计关于上架的
		foreach($arrDatas['start_time'] as $arrData){
			//获取类目
			if(empty($arrResult[$arrData['cat_id']])){
				$arrResult[$arrData['cat_id']]['name'] = $arrCategoryList[$arrData['cat_id']];
			}
			if(!$arrData['admin_id']){
				continue;
			}					
			//获取用户名
			if(empty($arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['username'])){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['username'] = $arrUserList[$arrData['admin_id']]['real_name'];
			}
			//总的上架数
			$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['shangjia_sum']++;
			if($arrData['promo_price'] < 10){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['shangjia_l_10']++;
			}
			if($arrData['promo_price'] >= 10 && $arrData['promo_price'] < 20){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['shangjia_10_20']++;
			}
			if($arrData['promo_price'] >= 20 && $arrData['promo_price'] < 30){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['shangjia_20_30']++;
			}
			if($arrData['promo_price'] >= 30){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['shangjia_g_30']++;
			}								
		}
		unset($arrDatas['start_time']);
/*----------------------------------------------------------------------------------------------------*/
		//统计关于销量的
		foreach($arrDatas['logtime'] as $arrData){
			//获取类目
			if(empty($arrResult[$arrData['cat_id']])){
				$arrResult[$arrData['cat_id']]['name'] = $arrCategoryList[$arrData['cat_id']];
			}
			if(!$arrData['admin_id']){
				continue;
			}					

			//获取用户名
			if(empty($arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['username'])){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['username'] = $arrUserList[$arrData['admin_id']]['real_name'];
			}
			//我选的这个时间范围然后成交不足10单的。
			if($arrData['turnover'] < 10){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['turnover_l_10']++;
			}
			//我选的这个时间范围然后成交大于150单的。
			if($arrData['turnover'] > 150){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['turnover_g_150']++;
			}

			//10元以内的销量
			if($arrData['promo_price'] < 10){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['promo_price_l_10']+=$arrData['turnover'];
			}
			//10-20元以内的销量
			if($arrData['promo_price'] >= 10 && $arrData['promo_price'] < 20){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['promo_price_10_20']+=$arrData['turnover'];
			}
			//20-30元以内的销量
			if($arrData['promo_price'] >= 20 && $arrData['promo_price'] < 30){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['promo_price_20_30']+=$arrData['turnover'];
			}
			//30以上
			if($arrData['promo_price'] >= 30){
				$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['promo_price_g_30']+=$arrData['turnover'];
			}
			//总的销量
			$arrResult[$arrData['cat_id']]['data'][$arrData['admin_id']]['promo_price_sum']+=$arrData['turnover'];								
		}
		unset($arrDatas['logtime']);
/*----------------------------------------------------------------------------------------------------*/

		//print_r($arrResult);exit;
		$this->assign($_GET);
		$this->assign('data', $arrResult);
        $this->render();   				
	}

	public function actionCounttotal(){
		//默认的没有其它搜索条件选择时间是昨天凌晨开始
		if(empty($_GET['start_time']) && empty($_GET['end_time'])){
            $start_time = strtotime('yesterday');//昨天凌晨到今天的凌晨的时间
            $end_time = strtotime('today');            
		}else{
			$start_time = empty($_GET['start_time']) ? 0 : strtotime($_GET['start_time']);
			$end_time = empty($_GET['end_time']) ? 0 : strtotime($_GET['end_time']) + 86400;
		}
/*----------------------------------------------------------------------------------------------------*/

		//根据时间范围选取各个类目数据
		$select = 'sum(sales) sales,sum(turnover) turnover,admin_id,cat_id';
		$arrDatas = GoodsBmModel::model()->getDataForAdd_time($start_time, $end_time, 'logtime', $select, "admin_id > 0", 'GuestFenxiModel', array('group' => 'admin_id'));

        $arrResult = array();
		$arrUserList = AdminUser::model()->getUserList(); //获取用户名
		$arrCategoryList = CategoryModel::model()->getList(); //获取分类的名字 
        foreach($arrDatas as $key => $arrData){
			$arrData['name'] = $arrCategoryList[$arrData['cat_id']]['cat_name'];
			$arrData['quantify'] = $arrCategoryList[$arrData['cat_id']]['quantify'];
			$arrData['single_quantity'] = $arrCategoryList[$arrData['cat_id']]['single_quantity'];			
			$arrData['username'] = $arrUserList[$arrData['admin_id']]['real_name'];        	
        	$arrResult[$arrData['cat_id']][] = $arrData;
        }
        unset($arrDatas);
		$this->assign($_GET);
		$this->assign('data', $arrResult);
        $this->render();   	
	}
}